//
//  SearchHeadView.m
//  MedicalCare
//
//  Created by lbh on 2021/10/11.
//  Copyright © 2021 hs. All rights reserved.
//

#import "SearchHeadView.h"

@implementation SearchHeadView

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        [self makeUI];
    }
    return self;
}

#pragma maek --Private
- (void)makeUI {

    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.left.mas_equalTo(6);
        make.right.mas_equalTo(-6);
        make.height.mas_equalTo(36);
    }];

    [self.bgView addSubview:self.searchBgView];
    [self.searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-56);
        make.bottom.mas_equalTo(0);
    }];

//    [self.searchBgView addSubview:self.searchImageView];
//    [self.searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.searchBgView.mas_centerY);
//        make.left.mas_equalTo(12);
//        make.size.mas_equalTo(CGSizeMake(24, 24));
//    }];

    [self.searchBgView addSubview:self.searchTextField];
    [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.searchBgView.mas_centerY);
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH - 70 - 10 - 76);
    }];
    
    [self.searchBgView addSubview:self.clearButton];
    [self.clearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.searchTextField.mas_centerY);
        make.right.mas_equalTo(-6);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    self.cancelBtn.layer.cornerRadius = 15;
    self.cancelBtn.layer.masksToBounds = YES;
    [self.cancelBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHex:@"#2BA3FF"]] forState:UIControlStateNormal];
    
    [self.bgView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.right.mas_equalTo(-4);
        make.height.equalTo(@(30));
        make.width.equalTo(@(50));
    }];
}
- (void)skTagTapMethod:(NSString *)tagStr{
    self.searchTextField.text = tagStr;
    [self cancelSelectStatus];
}
- (void)cancelSelectStatus{
    NSString *text = self.searchTextField.text;
    if ([NSString isEmpty:text]) {
        _clearButton.hidden = YES;
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    }
    else{
        _clearButton.hidden = NO;
        [_cancelBtn setTitle:@"搜索" forState:UIControlStateNormal];
    }
}
//source:YES顶部筛选 NO:右下角
- (void)searchTapMethod:(BOOL)source{
    NSString *title = self.cancelBtn.currentTitle;
    if ([title isEqualToString:@"搜索"]) {
        [self.searchTextField resignFirstResponder];
        !self.searchHeadViewBlock?:self.searchHeadViewBlock(3);
    }
    else{
        if (source) {
            !self.searchHeadViewBlock?:self.searchHeadViewBlock(2);
        }
    }
}
#pragma mark --Action
- (void)didChangeValue:(UITextField *)textField{
    NSString *text = textField.text;
    if ([NSString isEmpty:text]) {
        self.clearButton.hidden = YES;
        !self.searchHeadViewBlock?:self.searchHeadViewBlock(1);
    }
    else{
        self.clearButton.hidden = NO;
        !self.searchHeadViewBlock?:self.searchHeadViewBlock(0);
    }
    [self cancelSelectStatus];
}
- (void)cancelBtnAction:(UIButton *)btn {
    [self searchTapMethod:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self searchTapMethod:NO];
    return YES;
}
- (void)cleaaButton:(UIButton *)button{
    [[KFTools getCurrentVC].view endEditing:YES];
    self.searchTextField.text = @"";
    self.clearButton.hidden = YES;
    [self cancelSelectStatus];
    !self.searchHeadViewBlock?:self.searchHeadViewBlock(1);
}
#pragma mark --Lazy
- (UIView *)bgView {

    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor clearColor];
    }
    return _bgView;
}
- (UIView *)searchBgView {

    if (!_searchBgView) {
        _searchBgView = [[UIView alloc]init];
        _searchBgView.backgroundColor = [UIColor colorWithHex:@"#FFFFFF"];
        _searchBgView.layer.masksToBounds = YES;
        _searchBgView.layer.cornerRadius = 18;
    }
    return _searchBgView;
}
- (UIImageView *)searchImageView {

    if (!_searchImageView) {
        _searchImageView = [[UIImageView alloc]init];
        _searchImageView.image = [UIImage imageNamed:@"home_icon_search"];
    }
    return _searchImageView;
}
- (UITextField *)searchTextField {

    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc] init];
        _searchTextField.placeholder = @"搜索其实很简单";
        [_searchTextField setValue:[UIColor colorWithHex:@"#999999"] forKeyPath:@"placeholderLabel.textColor"];
        [_searchTextField setValue:kFont(14) forKeyPath:@"placeholderLabel.font"];
        _searchTextField.layer.masksToBounds = YES;
        [_searchTextField addTarget:self action:@selector(didChangeValue:) forControlEvents:UIControlEventEditingChanged];
        _searchTextField.font = kFont(14);
        _searchTextField.textColor = [UIColor colorWithHex:@"#999999"];
        _searchTextField.textAlignment = NSTextAlignmentLeft;
        _searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _searchTextField.borderStyle = UITextBorderStyleNone;
        _searchTextField.returnKeyType = UIReturnKeySearch;//变为搜索按钮
        _searchTextField.delegate = self;//设置代理
    }
    return _searchTextField;
}
- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor colorWithHex:@"#FFFFFF"] forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = kFont(16);
        [_cancelBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
- (UIButton *)clearButton{
    if (!_clearButton) {
        _clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_clearButton setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
        [_clearButton addTarget:self action:@selector(cleaaButton:) forControlEvents:UIControlEventTouchUpInside];
        _clearButton.hidden = YES;
    }
    return _clearButton;
}
@end
